package com.xidian.zhiancloud.controller.emergencyManagement;

import com.xidian.zhiancloud.constant.EmergencyManagementConstant;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dao.EmergencyPlanManagementMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.EmergencyPlanExec;
import com.xidian.zhiancloud.model.EmergencyPlanManagement;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.service.emergencyManagement.EmergencyPlanExecService;
import com.xidian.zhiancloud.service.user.UserService;
import com.xidian.zhiancloud.service.user.usermanage.OrganizationService;
import com.xidian.zhiancloud.util.zyf.ExcelUtils;
import com.xidian.zhiancloud.vo.emergencyManagement.EmergencyPlanExecVO;
import com.xidian.zhiancloud.vo.emergencyManagement.EmergencyPlanManagementVO;
import com.xidian.zhiancloud.vo.emergencyManagement.EmergencyTeamManagementDTOVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * 应急管理 预案执行记录接口
 *
 */
@RestController
@RequestMapping("/EmergencyPlanExec")
public class EmergencyPlanExecController extends BaseController {

    @Autowired
    private EmergencyPlanExecService emergencyPlanExecService;

    @Autowired
    private UserService userService;

    @Autowired
    private EmergencyPlanManagementMapper emergencyPlanManagementMapper;
    /**
     * 根据id获取应急执行记录对象
     *
     * @param id 应急预案对象的id
     * @return 应急执行记录对象
     */
    @GetMapping("/getListById")
    public ResultDTO<List<EmergencyPlanExecVO>> getListById(@RequestParam("id") Long id) {
        return ResultDTO.successOf(emergencyPlanExecService.getListById(id), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 导出
     *
     * @param response
     * @param ids      id列表
     * @throws IOException
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response, Long[] ids) throws IOException {
        List<EmergencyPlanExec> list = emergencyPlanExecService.listByIds(Arrays.asList(ids));
        List<EmergencyPlanExec> ans = new LinkedList<>();
        list.forEach(o -> {
            EmergencyPlanExecVO vo =new EmergencyPlanExecVO();
            BeanUtils.copyProperties(o, vo);
            EmergencyPlanManagement emergencyPlanManagement=emergencyPlanManagementMapper.selectById(vo.getEmergencyPlanId());
            vo.setPlanName(emergencyPlanManagement==null?"该预案不存在":emergencyPlanManagement.getName());
            SysUser adder= userService.getById(vo.getStartUser());
            vo.setStartUserName(adder==null?"该用户不存在":adder.getName());
            SysUser ender= userService.getById(vo.getEndUser());
            vo.setEndUserName(ender==null?"该用户不存在":ender.getName());
            ans.add(vo);
        });
        ExcelUtils.exportExcel(ans, "预案执行记录", "预案执行记录", EmergencyPlanExecVO.class, "预案执行记录", response);
    }

}
